importScripts(
  "https://storage.googleapis.com/workbox-cdn/releases/3.3.1/workbox-sw.js"
);

workbox.setConfig({
  debug: true
});

workbox.precaching.precacheAndRoute([]);

workbox.skipWaiting();
workbox.clientsClaim();

self.addEventListener("activate", () => {
  postM("已激活！");
});

function postM(msg) {
  self.clients.matchAll().then(function(clients) {
    if (clients && clients.length) {
      clients.forEach(function(client) {
        client.postMessage(msg);
      });
    }
  });
}

self.addEventListener("push", e => {
  console.log("push", e);
  const data = e.data.json();
  console.log(data);
  const options = {
    icon: data.icon,
    body: data.body
  };
  self.registration.showNotification(data.title, options);
});

self.addEventListener("notificationclick", e => {
  console.log(e);
  e.notification.close();
  self.clients.openWindow('/') // 如果有clienr的话，则focuse

});
